import {
  Body,
  Controller,
  Get,
  Logger,
  Param,
  Post,
  // UseGuards,
} from '@nestjs/common';
import { UserService } from './user.service';
import { User } from './entities/user.entity';
import { LoginUserDto } from '../auth/dto/signin-user.dto';

@Controller('users')
export class UserController {
  constructor(
    private userService: UserService,
    private readonly logger: Logger,
  ) {}

  @Get('/findUserById/:id')
  async findUserById(@Param('id') id: number) {
    return this.userService.find(undefined, id);
  }

  @Post('/login')
  async login(@Body() user: LoginUserDto) {
    const userExist = await this.userService.find(user.username);

    if (!userExist) {
      const register = await this.userService.create({
        username: user.username,
        password: user.password,
        roles: [2],
      });

      if (register) {
        return {
          username: register.username,
          token: 'token',
        };
      }
    }

    return {
      username: userExist.username,
      token: 'token',
    };
  }

  @Post('/register')
  async register(@Body() user: User) {
    return this.userService.create({
      username: user.username,
      password: user.password,
      roles: [2],
    });
  }
}
